package com.decent.ejfadmin.collectcard.response;

import com.decent.ejfadmin.common.enums.SupplyCardFailReason;
import com.decent.ejfadmin.common.enums.SupplyCardStatus;
import com.decent.ejfadmin.common.util.Constants;
import com.decent.ejfadmin.utils.Des;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.*;

/**
 * 收卡明细查询返回实体
 *
 * @author weicx
 * @date 2018/08/02
 */
@Getter
@Setter
public class CollectCardDetailDto {
    public static final List<Integer> VIRTUAL = new ArrayList<>(Arrays.asList(15, 16, 20));
    public static final List<Integer> FUELCARD = new ArrayList<>(Arrays.asList(300, 302, 303, 310));
    private static final HashMap<Integer, String> SUPPLY_CARD_STATUS = new HashMap<Integer, String>() {{
        put(0, "在售");
        put(1, "已售");
        put(2, "停售");
        put(3, "可售");
        put(4, "未预判面值");
        put(5, "预判面值不符");
        put(7, "等待慢速试充");
        put(8, "慢速试充");
        put(50, "补结算");
    }};
    /**
     * 自增id
     */
    private Long id;
    /**
     * 会员编号
     */
    private String userAccount;
    /**
     * 会员名称
     */
    private String userName;
    /**
     * 卡号
     */
    private String cardNumber;
    /**
     * 卡密
     */
    private String cardPwd;
    /**
     * 状态
     */
    private Integer status;
    /**
     * 面值
     */
    private Double face;
    /**
     * 预判面值
     */
    private Integer judgeFace;
    /**
     * 收卡时间
     */
    private String startTime;
    /**
     * 销卡时间
     */
    private String endTime;
    /**
     * 失效时间
     */
    private String effectTime;
    /**
     * 运营商
     */
    private Integer productType;
    /**
     * 折扣
     */
    private BigDecimal discount;
    /**
     * 销卡面值
     */
    private BigDecimal settlePrice;
    /**
     * 失败原因
     */
    private String failReason;
    /**
     * 批次号
     */
    private String batchId;
    /**
     * 补结算
     */
    private Integer added;
    /**
     * 地区
     */
    private String area;
    /**
     * 结算金额
     */
    private BigDecimal userPrice;
    /**
     * 1 ：未预判且产品维护  0：其他
     */
    private Integer maintenance;
    /**
     * 对内订单号
     */
    private String orderId;
    /**
     * 商户编号
     */
    private Integer agentNo;
    /**
     * 订单来源
     */
    private String cardSource;
    private String orderSource;
    /**
     * 手机号/充值账号
     */
    private String mobileNo;
    /**
     * 产品Id
     */
    private Long productId;
    /**
     * 产品名称
     */
    private String productName;
    /**
     * 运营商名称
     */
    private String cardOperatorName;
    /**
     * 权益销卡面值
     */
    private BigDecimal equitySettlePrice;
    /**
     * 权益结算金额
     */
    private BigDecimal equityUserPrice;
    /**
     * 地区(查运营商用)
     */
    private String supplyArea;
    /**
     * 茶蛋订单号
     */
    private String teeEggOrderNo;
    /**
     * 备注
     */
    private String remark;
    /**
     * 卡券图片密钥
     */
    private String aliKey;
    /**
     * 卡券图片
     */
    private String picUrl;
    /**
     * 会员Id
     */
    private Long userId;

    public String toDownloadString(boolean downloadCardPwd, String pwdKey) throws UnsupportedEncodingException {
        final StringBuilder csv = new StringBuilder(500);
        csv.append("=\"").append(cardNumber).append("\"").append(",");
        if (downloadCardPwd) {
            csv.append("=\"")
                    .append(StringUtils.trimToEmpty(Des.reserveDecode(pwdKey, URLDecoder.decode(cardPwd, "UTF-8"))))
                    .append("\",");
        }
        if (getAdded().equals(1)) {
            csv.append("补结算").append(",");
        } else {
            Integer dbStatus = status;
            csv.append(SUPPLY_CARD_STATUS.get(dbStatus)).append(",");
        }
        csv.append("=\"").append(batchId).append("\"").append(",");
        csv.append(face).append(",");
        csv.append("=\"").append(startTime == null ? "" : startTime).append("\"").append(",");
        csv.append("=\"").append(endTime == null ? "" : endTime).append("\"").append(",");
        csv.append(StringUtils.isNotBlank(cardOperatorName) ? cardOperatorName : "").append(",");
        csv.append(discount == null ? "" : (productType > 310 ? BigDecimal.valueOf(1) : discount)).append(",");
        csv.append(settlePrice == null ? "" : settlePrice).append(",");
        csv.append((!Integer.valueOf(1).equals(status) ? BigDecimal.ZERO : (productType > 310 ? equityUserPrice.toString() : new BigDecimal(discount.toString())
                .multiply(new BigDecimal(settlePrice.toString()))))).append(",");
        csv.append(StringUtils.isBlank(area) ? "" : area).append(",");
        csv.append(StringUtils.isBlank(SupplyCardFailReason.REASON_MAP
                .get(String.valueOf(failReason))) ? StringUtils.trimToEmpty(failReason) : StringUtils.trimToEmpty(SupplyCardFailReason.REASON_MAP
                .get(String.valueOf(failReason)))).append(",");
        if (Objects.nonNull(agentNo) && Constants.EPAY.contains(agentNo)) {
            csv.append("H").append(",");
        } else if (Objects.nonNull(agentNo) && Constants.ESERVER.contains(agentNo)) {
            csv.append("D").append(",");
        } else if (Objects.equals(status, SupplyCardStatus.success.getStatus()) && VIRTUAL.contains(productType)) {
            csv.append("X").append(",");
        } else if (Objects.equals(status, SupplyCardStatus.success.getStatus()) && FUELCARD.contains(productType)) {
            csv.append("J").append(",");
        } else {
            csv.append("-").append(",");
        }
        csv.append("=\"").append(StringUtils.isBlank(mobileNo) ? "" : mobileNo).append("\"").append(",");
        csv.append("=\"").append(StringUtils.isBlank(orderId) ? "" : orderId).append("\"");
        csv.append("\r\n");
        return csv.toString();
    }
}
